package com.kzd.sdk.ui.fragment;

import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.kzd.sdk.base.OwnerFragment;
import com.kzd.sdk.callback.KZD_HttpCallback;
import com.kzd.sdk.callback.KZD_OnClickListener;
import com.kzd.sdk.core.KZD_ApiConstance;
import com.kzd.sdk.core.KZD_ApiManager;
import com.kzd.sdk.core.KZD_SdkController;
import com.kzd.sdk.entity.KZD_UserInfo;
import com.kzd.sdk.ui.activity.KZD_AccessActivity;
import com.kzd.sdk.ui.activity.KZD_UserGiftPackActivity;
import com.kzd.sdk.ui.activity.KZD_UserPassActivity;
import com.kzd.sdk.ui.activity.KZD_UserPhoneActivity;
import com.kzd.sdk.ui.activity.KZD_UserRebateActivity;
import com.kzd.sdk.ui.activity.KZD_UserRechargeActivity;
import com.kzd.sdk.ui.activity.KZD_UserServiceActivity;
import com.kzd.sdk.ui.activity.KZD_UserSmallActivity;
import com.kzd.sdk.ui.activity.KZD_UserVerifyActivity;
import com.kzd.sdk.ui.dialog.KZD_AccountSwitchDialog;
import com.kzd.sdk.utils.AppUtils;

public class KZD_HomeUserFragment extends OwnerFragment {

    private ImageView ivUserHeader;
    private TextView tvUserName, tvUserAccount, tvUserMoney, tvUserPhone, tvUserRealInfo,tvUserChangePwd;
    private KZD_AccountSwitchDialog mAccountSwitchDialog;

    @Override
    public String getLayoutIdByName() {
        return "kzd_fragment_home_user";
    }

    @Override
    public void initView(View view) {

        ivUserHeader = view.findViewById(AppUtils.getId(mContext, "ivUserHeader"));
        tvUserName = view.findViewById(AppUtils.getId(mContext, "tvUserName"));
        tvUserAccount = view.findViewById(AppUtils.getId(mContext, "tvUserAccount"));
        tvUserMoney = view.findViewById(AppUtils.getId(mContext, "tvUserMoney"));
        tvUserPhone = view.findViewById(AppUtils.getId(mContext, "tvUserPhone"));
        tvUserRealInfo = view.findViewById(AppUtils.getId(mContext, "tvUserRealInfo"));

        mAccountSwitchDialog = new KZD_AccountSwitchDialog(mContext);

        //切换账号确认点击事件
        mAccountSwitchDialog.setOnClickListener(new KZD_OnClickListener() {
            @Override
            public void onClick(View v) {
                KZD_SdkController.getOnHideListener().onAccountSwitch();
                KZD_SdkController.getOnLoginListener().onLoginSwitch(KZD_ApiConstance.LOGIN_MAIN);
                KZD_AccessActivity.open();
                if (getActivity() != null) {
                    getActivity().finish();
                }
            }
        });

        //立即充值 点击事件
        view.findViewById(AppUtils.getId(mContext, "tvUserRecharge"))
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        KZD_UserRechargeActivity.open(mContext);
                    }
                });

        //申请返利 点击事件
        view.findViewById(AppUtils.getId(mContext, "tvUserApplyRebate"))
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        KZD_UserRebateActivity.open(mContext);
                    }
                });

        //我的礼包 点击事件
        view.findViewById(AppUtils.getId(mContext, "tvUserMyGift"))
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        KZD_UserGiftPackActivity.open(mContext);
                    }
                });

        //小号管理 点击事件
        view.findViewById(AppUtils.getId(mContext, "tvUserSmallAccount"))
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        KZD_UserSmallActivity.open(mContext);
                    }
                });

        //客服中心 点击事件
        view.findViewById(AppUtils.getId(mContext, "tvUserServiceCenter"))
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        KZD_UserServiceActivity.open(mContext);
                    }
                });

        //消息 点击事件
        view.findViewById(AppUtils.getId(mContext, "tvUserMessage"))
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    }
                });

        //绑定手机 点击事件
        view.findViewById(AppUtils.getId(mContext, "flUserBindPhone"))
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        KZD_UserInfo userInfo = KZD_SdkController.getUserInfo();
                        if (userInfo == null) {
                            return;
                        }
                        if (TextUtils.isEmpty(userInfo.getMobile())) {
                            KZD_UserPhoneActivity.open(mContext);
                        }
                    }
                });

        //实名信息 点击事件
        view.findViewById(AppUtils.getId(mContext, "flUserRealVerified"))
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        KZD_UserVerifyActivity.open(mContext);
                    }
                });

        //修改密码 点击事件
        tvUserChangePwd = view.findViewById(AppUtils.getId(mContext, "tvUserChangePwd"));
        tvUserChangePwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                KZD_UserPassActivity.open(mContext);
            }
        });

        //切换账号 点击事件
        view.findViewById(AppUtils.getId(mContext, "tvUserSwitchAccount"))
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mAccountSwitchDialog.show();
                    }
                });

        //打开GT游戏盒子 点击事件
        view.findViewById(AppUtils.getId(mContext, "flUserOpenGTGame"))
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (!AppUtils.isAppInstalled(mContext, KZD_ApiConstance.GT_GAME_PACKAGE)) {
                            AppUtils.launchBrowser(mContext, KZD_ApiConstance.GT_GAME_WEB);
                        } else {
                            AppUtils.launchApp(mContext, KZD_ApiConstance.GT_GAME_PACKAGE);
                        }
                    }
                });
    }

    @Override
    public void initData() {
//        KZD_ApiManager.httpUserInfo(new KZD_HttpCallback<KZD_UserInfo>() {
//            @Override
//            public void onSuccess(KZD_UserInfo data) {
//                fillData(data);
//            }
//        });
    }

    @Override
    public void onResume() {
        super.onResume();
        KZD_ApiManager.httpUserInfo(new KZD_HttpCallback<KZD_UserInfo>() {
            @Override
            public void onSuccess(KZD_UserInfo data) {
                fillData(data);
            }
        });
        fillData(KZD_SdkController.getUserInfo());
    }

    private void fillData(KZD_UserInfo userInfo) {
        if (userInfo == null) {
            return;
        }
        int resId = AppUtils.getDrawableId(mContext, "kzd_ic_head_portrait");
        ivUserHeader.setImageResource(resId);
        tvUserName.setText(TextUtils.isEmpty(userInfo.getPet_name()) ? userInfo.getUser_name() : userInfo.getPet_name());
        tvUserAccount.setText(new StringBuilder("账号：").append(userInfo.getUser_name()));
        tvUserMoney.setText(new StringBuilder("金币余额：").append(userInfo.getCoin()));
        tvUserPhone.setText(TextUtils.isEmpty(userInfo.getMobile()) ? "未绑定" : userInfo.getMobile());
        tvUserRealInfo.setText(userInfo.getVerify_info() != 1 ? "未实名" : "已实名");
        if (userInfo.getIs_pwd() == 1) {
            tvUserChangePwd.setText("修改密码");
        } else {
            tvUserChangePwd.setText("设置密码");
        }
    }
}
